var dateTimePicker = require('../../utils/dateTimePicker.js');
var common = require('../../utils/common.js');
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    item:[],
    date: '',
    time: '',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2020,
    endYear: 2050,
    inputTxt: '输入接龙活动标题',
    textareaTxt: '输入简短的推荐语…',
    solitaireName: '',
    pickerShow: false,
    solitaire:[],
    hour: [],
    index: 47,
    solitaireTxt: '',
    thisHour: ''
  },
  onLoad: function (options) {
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data)
      that.setData({
        item: data.data,
      })
    })
    var hour = []
    for(let i=0;i<48;i++){
      hour.push(i+1+'小时')
      this.setData({
        hour:hour
      })
      if(i == 47){
        this.setData({
          thisHour: i+1
        })
      }
    }
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },
  changeDate(e) {
    this.setData({ 
      date: e.detail.value + ' 23:59:59'
    });
    console.log(this.data.date)
  },
  changeTime(e) {
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e) {
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
    console.log(dateTime)
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  inputWacth: function (e) {
    console.log(e);
    this.setData({
      solitaireName: e.detail.value
    });
  },
  textareaWacth(e){
    console.log(e);
    this.setData({
      solitaireTxt: e.detail.value
    });
  },
  create(){
    const that = this
    if (this.data.solitaireName == ''){
      wx.showToast({
        title: '活动名称不能为空',
        icon: 'none',
        duration: 2000
      })
    } else{
      wx.showLoading({
        title: '接龙活动创建中',
        mask: true
      })
      var time = Date.parse(new Date());
      var thistime = time + this.data.thisHour * 3600 * 1000
      console.log(thistime)
      console.log(util.formatTimeTwo(thistime, 'Y-M-D h:m:s'))
      var endTime = util.formatTimeTwo(thistime, 'Y-M-D h:m:s')
      common.post({
        url: '/app/v2/skusolitaire/begin', data: { sessionId: app.globalData.sessionId, skuId: this.data.item.skuId, title: this.data.solitaireName, endTime: endTime, description: this.data.solitaireTxt}
      })
        .then((res) => {
          console.log(res.data)
          if (res.data.code == 0) {
            
            that.setData({
              solitaire: res.data.data
            })
            var path = '/pages/solitaireDetailed/solitaireDetailed?solitaireId=' + res.data.data.solitaireId + '&userId=' + app.globalData.userInfo.userId
            console.log(path)
            wx.redirectTo({
              url: path,
              success(){
                wx.hideLoading()
              }
            })
          } else if (res.data.code == "-9998") {
            wx.hideLoading()
            that.loginTip()
          } else if (res.data.code == "-4"){
            wx.hideLoading()
            wx.showModal({
              title: '提示',
              content: res.data.errmsg,
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
          } else {
            console.log(res)
          }
        }).catch((res) => {
          console.log(res)
        })
    }
    
  },
  changeHour(e){
    console.log(e)
    this.setData({
      index: e.detail.value,
      thisHour: parseInt(this.data.hour[e.detail.value]),
      date: '结束时间'
    })
    console.log(this.data.thisHour)
    
  }
})